<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="600">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules"
             label-width="90px" :disabled="disabled">
      <el-form-item label="厂商编码" prop="code">
        <el-input v-model="formData.code" placeholder="请输入厂商编码" clearable/>
      </el-form-item>
      <el-form-item label="厂商名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入厂商名称" clearable/>
      </el-form-item>
      <el-form-item label="根域名" prop="baseUrl">
        <el-input v-model="formData.baseUrl" placeholder="请输入厂商根域名" clearable/>
      </el-form-item>
      <el-form-item label="远程开门" prop="isRemoteOpen">
        <el-select v-model="formData.isRemoteOpen" placeholder="请选择">
          <el-option v-for="dict in getBoolDictOptions(DICT_TYPE.YXT_SUPPLIER_STATUS)"
                     :label="dict.label" :value="dict.value  as boolean"/>
        </el-select>
      </el-form-item>
      <el-form-item label="常开门" prop="isKeepOpen">
        <el-select v-model="formData.isKeepOpen" placeholder="请选择">
          <el-option v-for="dict in getBoolDictOptions(DICT_TYPE.YXT_SUPPLIER_STATUS)"
                     :label="dict.label" :value="dict.value as boolean"/>
        </el-select>
      </el-form-item>
      <el-form-item label="人脸识别" prop="isFaceOpen">
        <el-select v-model="formData.isFaceOpen" placeholder="请选择">
          <el-option v-for="dict in getBoolDictOptions(DICT_TYPE.YXT_SUPPLIER_STATUS)"
                     :label="dict.label" :value="dict.value  as boolean"/>
        </el-select>
      </el-form-item>
      <el-form-item label="密码开门" prop="isPasswordOpen">
        <el-select v-model="formData.isPasswordOpen" placeholder="YXT_SUPPLIER_STATUS">
          <el-option v-for="dict in getBoolDictOptions(DICT_TYPE.YXT_SUPPLIER_STATUS)"
                     :label="dict.label" :value="dict.value  as boolean"/>
        </el-select>
      </el-form-item>
      <el-form-item label="蓝牙开门" prop="isBlueToothOpen">
        <el-select v-model="formData.isBlueToothOpen" placeholder="请选择">
          <el-option v-for="dict in getBoolDictOptions(DICT_TYPE.YXT_SUPPLIER_STATUS)"
                     :label="dict.label" :value="dict.value  as boolean"/>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" maxlength="500" show-word-limit
                  :rows="3"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import {useMessage} from "@/components/Notice";
import {useSupplierApi} from "@/api/yxt/supplier";
import {DICT_TYPE, getBoolDictOptions} from "@/utils/dict";

defineOptions({name: 'YxtSupplierForm'})

const {t} = useI18n();
const supplierApi = useSupplierApi();

const dialogVisible = ref<boolean>(false)
const dialogTitle = ref<string>('')
const formLoading = ref<boolean>(false)
const formType = ref<string>('')
const formData = ref<any>({
  id: undefined,
  code: '',
  name: '',
  baseUrl: '',
  isRemoteOpen: false,
  isKeepOpen: false,
  isFaceOpen: false,
  isPasswordOpen: false,
  isBlueToothOpen: false,
  remark: '',
})

const formRules = reactive({
  code: [{required: true, message: '编码不能为空', trigger: ['blur', 'change']}],
  name: [{required: true, message: '名称不能为空', trigger: ['blur', 'change']}],
  baseUrl: [{required: true, message: '根域名不能为空', trigger: ['blur', 'change']}],
})

const formRef = ref()
const disabled = ref<boolean>(false)

const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      formData.value = await supplierApi.getObj(id)
    } finally {
      formLoading.value = false;
      if (type === 'view') disabled.value = true;
    }
  }
}
defineExpose({open})

const emit = defineEmits(['success'])
const submitForm = () => {
  if (formType.value === 'view') {
    return dialogVisible.value = false
  }

  formRef.value?.validate(async (valid: any) => {
    if (valid) {
      formLoading.value = true
      try {
        const data = formData.value;
        if (formType.value === 'create') {
          await supplierApi.createObj(data);
          useMessage(t('common.createSuccess'));
        } else {
          await supplierApi.updateObj(data);
          useMessage(t('common.updateSuccess'));
        }
        dialogVisible.value = false
        emit('success')
      } finally {
        formLoading.value = false
      }
    }
  });
}

const resetForm = () => {
  disabled.value = false;
  formData.value = {
    id: undefined,
    code: '',
    name: '',
    baseUrl: '',
    isRemoteOpen: false,
    isKeepOpen: false,
    isFaceOpen: false,
    isPasswordOpen: false,
    remark: '',
  }
  formRef.value?.resetFields()
}
</script>
